package com.example.amusic.service

import androidx.media3.common.C
import androidx.media3.common.Player
import com.example.amusic.service.MediaConstants.DEFAULT_DURATION_MS
import com.example.amusic.service.MediaConstants.DEFAULT_INDEX
import com.example.amusic.service.MediaConstants.DEFAULT_MEDIA_ID

data class MusicState(
    val currentMediaId: String = DEFAULT_MEDIA_ID,
    val currentSongIndex: Int = DEFAULT_INDEX,
    val playbackState: PlaybackState = PlaybackState.IDLE,
    val playWhenReady: Boolean = false,
    val duration: Long = DEFAULT_DURATION_MS
)

enum class PlaybackState { IDLE, BUFFERING, READY, ENDED }
object MediaConstants {
    const val DEFAULT_MEDIA_ID = ""
    const val DEFAULT_INDEX = 0
    const val DEFAULT_POSITION_MS = 0L
    const val DEFAULT_DURATION_MS = 0L
}

internal fun Int.asPlaybackState() = when (this) {
    Player.STATE_IDLE -> PlaybackState.IDLE
    Player.STATE_BUFFERING -> PlaybackState.BUFFERING
    Player.STATE_READY -> PlaybackState.READY
    Player.STATE_ENDED -> PlaybackState.ENDED
    else -> error("Invalid playback state.")
}

internal fun Long.orDefaultTimestamp() = takeIf { it != C.TIME_UNSET } ?: DEFAULT_DURATION_MS
